%% ex3.m
% set dir: src/
close all;
clear; clc;

%% main
img = double(imread("../data/rose512.tif"));

% normalize
f = img ./ sum(img,'all');
F = dft2D(f);
g = real(idft2D(F));
d = f - g;
figure;
imshow(d,[])
axis off;
title('diff')
saveas(gcf, '../assets/diff.png', 'png')

%% dft2D
function F = dft2D(f)
[m,n] = size(f);
F = zeros(m,n);
for i=1:m
    F(i,:) = fft(f(i,:));
end
for j=1:n
    F(:,j) = fft(F(:,j));
end
end

%% idft2D
function f = idft2D(F)
[m, n] = size(F);
f = zeros(m, n);
for i=1:m
    f(i, :) = ifft(F(i,:));
end
for j=1:n
    f(:, j) = ifft(f(:, j));
end
end
